/****************************************************************************
*
* Description: Basic resource
* Created at: 19.11.06 21:07:47 UTC
*
* $Revision$
*
****************************************************************************/
#pragma once

#if !defined(__common_resource_h__)
#define __common_resource_h__

#include <string>
#include <ostream>
#include <istream>
#include <common/types.h>

namespace common {

/* Defines basic resource.
 * Each resource should have identifier and label.
 * Also class defines serialization and dump basics.  */
class basic_resource
{
protected:
	typedef	uint32	id_type;

public:
	/* Ctor. Resource should be initialized by ID.  */
    basic_resource(id_type id)
    	: id_(id)
    {}

	/* Copy ctor.  */
	basic_resource(const basic_resource& o)
		: id_(o.id_)
	{}

	/* Return resource ID.  */
    id_type& id()
    {
        return id_;
    }

	/* Debug stuff.  */
    virtual void dump(std::ostream& o)
    {
        o << "ID: " << id_ << std::endl;
    }

	/* Serialization stuff.  */
	/* TODO: may be we should use boost::serialization instead.
	 * boost::serialization has some issues with boost::*_ptr.  */
    virtual void serialize(std::ostream& o)
    {
        o << id_;
    }

    virtual void deserialize(std::istream& i)
    {
    	i >> id_;
    }

private:
	/* Data members.  */
    id_type         id_;
};

/* Basic resource type definition.  */
typedef basic_resource resource_t;

/* Dump any resource to stream. Thanks to polymorphism such
 * construction can be defined only once.  */
std::ostream& operator << (std::ostream& o, resource_t& r)
{
    r.dump(o);
    return o;
}

std::ostream& operator & (std::ostream& o, resource_t& r)
{
	r.serialize(o);
	return o;
}

std::istream& operator & (std::istream& i, resource_t& r)
{
	r.deserialize(i);
	return i;
}

} /* End of common namespace.  */

#endif  /* End of __common_resource_h__.  */
